package view;

import dao.UserInfoDao;
import entity.UserInfo;
import lombok.SneakyThrows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class LoginFrame {
    public LoginFrame() {
        JFrame jFrame = new JFrame("爬虫项目");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setBounds(500,350,400,150);
        jFrame.setLayout(new FlowLayout());
        JLabel userLable  = new JLabel("用户名");
        Dimension labeldimension = new Dimension(80,30);
        userLable.setPreferredSize(labeldimension);
        JTextField userNameFiled = new JTextField();
        Dimension textdimension = new Dimension(250,30);
        userNameFiled.setPreferredSize(textdimension);

        JLabel passwordLabel = new JLabel("密码");
        passwordLabel.setPreferredSize(labeldimension);
        JPasswordField passWordFiled = new JPasswordField();
        passWordFiled.setPreferredSize(textdimension);

        JButton jButton = new JButton("登录");
        jButton.setPreferredSize(new Dimension(100,30));

        jFrame.add(userLable);
        jFrame.add(userNameFiled);
        jFrame.add(passwordLabel);
        jFrame.add(passWordFiled);
        jFrame.add(jButton);

        jButton.addActionListener(new AbstractAction() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {
                String userName = userNameFiled.getText();
                String passWord = passWordFiled.getText();
                System.out.println(userName);
                System.out.println(passWord);
                UserInfo userInfo = UserInfoDao.login(userName,passWord);
                if (userInfo != null){
                    jFrame.setVisible(false);
                    new SelectFrame();
                }
                else {
                    JOptionPane.showMessageDialog(null,"用户名或密码错误");
                }
            }
        });


        jFrame.setVisible(true);
    }
}
